﻿#light

open NUnit.Framework
open Twitter.Model
open System

/// Intergration and unit testing

let username = "dazhurouclient"
let name = "dazhurouclienttest"
let password = "frtgty"

[<TestFixture>]
type TwitterLibIntegrationSpec () =

    let twitterStore = (AuthedTwitterStore(username, password) :> ITwitterAuthStore)
    
    [<TestFixtureSetUp>]
    member this.Setup = 
        ()
        
    [<Test>]
    member this.TestingWorks () =
        Assert.AreEqual(1, 1)
        
    [<Test>]
    member this.AuthenticationWorks () =
        Assert.AreEqual(true, twitterStore.TestAuth())
        
    [<Test>]
    member this.AuthenticationBadFails () =
        Assert.AreEqual(false, (AuthedTwitterStore(username, password + "fail") :> ITwitterAuthStore).TestAuth())
        
    [<Test>]
    member this.GetUserWorks () =
        let user = twitterStore.GetUser(username)
        Assert.AreEqual(name, user.Name)
        Assert.AreEqual(username, user.ScreenName)
                
    [<Test>]
    member this.GetUserBadFails () =
        Assert.Throws<System.Net.WebException>(new TestDelegate(fun () -> twitterStore.GetUser("zwwwz233szp") |> ignore)) |> ignore
        
    [<Test>]
    member this.GetStatusWorks () =
        let status = twitterStore.GetStatus(1582081934L)
        Assert.AreEqual("4/22/2009 4:32:51 AM", status.CreatedAt.ToString())

        Assert.AreEqual(1582081934L, status.Id)
        Assert.AreEqual("Finally at the hotel. Popping down to the gym. Miss my running through fields. So hard to keep fit on the road. X", status.Text)
    
    [<Test>]    
    member this.GetStatusBadFails () =
        Assert.Throws<System.Net.WebException>(new TestDelegate(fun () -> twitterStore.GetStatus(-20L) |> ignore)) |> ignore
    
    [<Test>]
    member this.SetStatusWorks () =
        let newStatus = twitterStore.SetStatus("Test status 你好")
        Assert.AreEqual("Test status 你好", newStatus.Text)
        
    [<Test>]
    member this.SetStatusReplyWorks () =
        let newStatus = twitterStore.SetStatus("@dazhurouclient Test reply status<a href=\"weep", 1618319253L)
        Assert.AreEqual("@dazhurouclient Test reply status&amp;lt;a href=\"weep", newStatus.Text)
        Assert.AreEqual(1618319253L, newStatus.InReplyToStatus)
        
    [<Test>]
    member this.DeleteStatusWorks () =
        let newStatus = twitterStore.SetStatus("to be deleted")
        twitterStore.DeleteStatus(newStatus.Id)
        Assert.Throws<System.Net.WebException>(new TestDelegate(fun () -> twitterStore.GetStatus(newStatus.Id) |> ignore)) |> ignore
        
    [<Test>]
    member this.GetFriendsWorks () =
        let friendListPage1 = twitterStore.GetFriends()
        let friendListPage2 = twitterStore.GetFriends("dazhurouclient", 2)
        Assert.AreEqual(21, friendListPage1.Length)
        Assert.AreEqual(0, friendListPage2.Length)
        
    [<Test>]
    member this.GetFollowersWorks () =
        let followerList = twitterStore.GetFollowers()
        Assert.AreEqual(2, followerList.Length)
        Assert.AreEqual("jokes972", followerList.[1].ScreenName)
        
    [<Test>]
    member this.GetArchiveWorks () =
        let archiveStatuses = twitterStore.GetArchive(fromId=1594607703L, toId=1610901088L)
        Assert.AreEqual(15, archiveStatuses.Length)
        
    [<Test>]
    member this.GetTimelineWorks () =
        let timelineStatuses = twitterStore.GetTimeline(count=5)
        Assert.AreEqual(5, timelineStatuses.Length)
        
    [<Test>]
    member this.GetDirectMessagesWorks () =
        let dms = twitterStore.GetDirectMessages(0L)
        Assert.AreEqual(2, dms.Length)
        
    [<Test>]
    member this.GetArchiveDirectMessagesWorks () =
        let adms = twitterStore.GetArchiveDirectMessages()
        Assert.AreEqual(0, adms.Length)

    [<Test>]
    member this.SetAndDeleteDMWorks () =
        let newDm = twitterStore.SetDirectMessage("dazhurouclient", "to be deleted")
        Assert.AreEqual("to be deleted", newDm.Text)
        twitterStore.DeleteDirectMessage(newDm.Id)
        Assert.DoesNotThrow(new TestDelegate((fun _ -> for dm in twitterStore.GetDirectMessages() do if dm.Id = newDm.Id then raise (new Exception()))))
    
    [<Test>]
    member this.CreateFriendshipWorks () =
        twitterStore.CreateFriendship("cnn")
        Assert.Throws<Exception>(new TestDelegate((fun _ -> for friend in twitterStore.GetFriends() do if friend.ScreenName = "cnn" then raise (new Exception())))) |> ignore
        
    [<Test>]
    member this.CreateFriendshipFails () =
        Assert.Throws<Net.WebException>(new TestDelegate((fun _ -> twitterStore.CreateFriendship("zaesdfezrrerdsf")))) |> ignore
    
    [<Test>]
    member this.EndFriendshipWorks () =
        twitterStore.EndFriendship("cnn")
        Assert.DoesNotThrow(new TestDelegate((fun _ -> for friend in twitterStore.GetFriends() do if friend.ScreenName = "cnn" then raise (new Exception())))) |> ignore
    
    [<Test>]
    member this.FriendshipExistsWorks () =
        Assert.AreEqual(true, twitterStore.FriendshipExists("dazhurouclient", "dazhurou"))
        
    [<Test>]
    member this.FriendshipExistsFails () =
        Assert.AreEqual(false, twitterStore.FriendshipExists("dazhurouclient", "cnn"))
        Assert.Throws<Net.WebException>(new TestDelegate((fun _ -> twitterStore.FriendshipExists("dazhurouclient", "ssqrzazaaze")|> ignore))) |> ignore
        
    [<Test>]
    member this.CreateBlockWorks () =
        Assert.DoesNotThrow(new TestDelegate((fun _ -> twitterStore.CreateBlock("dazhurou")))) |> ignore
        
    [<Test>]    
    member this.CreateBlockFails () =
        Assert.Throws<Net.WebException>(new TestDelegate((fun _ -> twitterStore.CreateBlock("fsdfsfezze")))) |> ignore
        
    [<Test>]
    member this.DestroyBlockWorks () =
        Assert.DoesNotThrow(new TestDelegate((fun _ -> twitterStore.DestroyBlock("dazhurou")))) |> ignore
        
    [<Test>]
    member this.GetFavoritesWorks () =
        Assert.AreEqual(3, twitterStore.GetFavorites().Length)
        
    [<Test>]
    member this.CreateFavoriteWorks () =
        twitterStore.CreateFavorite(1582081934L)
        Assert.Throws<Exception>(new TestDelegate((fun _ -> for fav in twitterStore.GetFavorites() do if fav.Id = 1582081934L then raise (new Exception())))) |> ignore
    
    [<Test>]
    member this.DeleteFavoriteWorks () =
        twitterStore.DestroyFavorite(1582081934L)
        Assert.DoesNotThrow(new TestDelegate((fun _ -> for fav in twitterStore.GetFavorites() do if fav.Id = 1582081934L then raise (new Exception()))))
        
    [<Test>]
    member this.SetUserWorks () =
        twitterStore.SetUser(description="description1", location="home")
        Assert.AreEqual("description1", twitterStore.GetUser(username).Description)
        Assert.AreEqual("home", twitterStore.GetUser(username).Location)
        
        twitterStore.SetUser(description="description2", location="work")
        Assert.AreEqual("description2", twitterStore.GetUser(username).Description)
        Assert.AreEqual("work", twitterStore.GetUser(username).Location)

[<TestFixture>]
type RequestBuilderSpec () =

    let requestBuilder = RequestBuilder(username, password)
    
    [<TestFixtureSetUp>]
    member this.Setup = 
        ()
        
    [<Test>]
    member this.BuildGetRequestWorks () =
        let request = requestBuilder.BuildGetRequest("http://test.com/chicken.xml", [("param1", "argument"); ("param2", "argument2")])
        Assert.AreEqual("GET", request.Method)
        Assert.AreEqual("http://test.com/chicken.xml?param1=argument&param2=argument2", request.RequestUri.ToString())
        Assert.IsNotEmpty(request.Headers)
        
    [<Test>]
    member this.BuildPostRequestWorks () =
        let request = requestBuilder.BuildPostRequest("http://test.com/chicken.xml", [("param1", "argument"); ("param2", "arguemnt2")])
        Assert.AreEqual("POST", request.Method)
        Assert.AreEqual("http://test.com/chicken.xml", request.RequestUri.ToString())
        Assert.AreEqual(32, request.ContentLength)
        
[<TestFixture>]
type TwitterXMLParserSpec () =
    
    [<TestFixtureSetUp>]
    member this.Setup = 
        ()
        
    [<Test>]
    member this.ParseUserWorks () =
        let user = TwitterXMLParser.ParseUser(Data.userXML)
        Assert.AreEqual("Maximillian", user.Name)
        Assert.AreEqual("dazhurou", user.ScreenName)
        Assert.AreEqual("http://s3.amazonaws.com/twitter_production/profile_images/146790097/profile_normal.png", user.ProfileImageURL)
        Assert.AreEqual("Shanghai",user.Location)
        Assert.AreEqual(false, user.ProtectedProfile)
        Assert.AreEqual(29, user.FollowersCount)
        
    [<Test>]
    member this.ParseStatusWorks() =
        let status = TwitterXMLParser.ParseStatus(Data.statusXML)
        Assert.AreEqual("4/22/2009 4:32:51 AM", status.CreatedAt.ToString())
        Assert.AreEqual("Imogen Heap", status.Owner.Name)
        Assert.AreEqual(1582081934L, status.Id)
        Assert.AreEqual("Finally at the hotel. Popping down to the gym. Miss my running through fields. So hard to keep fit on the road. X", status.Text)
        Assert.AreEqual(false, status.Favorited)
        Assert.AreEqual(-1, status.InReplyToStatus)
    
    [<Test>]
    member this.ParseDirectMessageWorks () =
        let dm = TwitterXMLParser.ParseDirectMessage(Data.dmXML)
        Assert.AreEqual(96378100L, dm.Id)
        Assert.AreEqual("Thks for this. You can send me an email : ", dm.Text)
        Assert.AreEqual(28297900L, dm.RecipientId)
        Assert.AreEqual("4/17/2009 3:18:16 AM", dm.CreatedAt.ToString())
    
    [<Test>]
    member this.ParseDirectMessageListWorks () =
        let dms = TwitterXMLParser.ParseDirectMessageList(Data.dmListXML)
        Assert.AreEqual(5, dms.Length)
        Assert.AreEqual(96378100L, dms.[0].Id)
        
    [<Test>]
    member this.ParseUserListWorks () =
        let dms = TwitterXMLParser.ParseUserList(Data.userListXML)
        Assert.AreEqual(21, dms.Length)
        Assert.AreEqual("dazhurou", dms.[0].ScreenName)
        
    [<Test>]
    member this.ParseStatusListWorks () =
        let statuses = TwitterXMLParser.ParseStatusList(Data.statusListXML)
        Assert.AreEqual(5, statuses.Length)
        Assert.AreEqual(1610900711L, statuses.[1].Id)
        
    [<Test>]
    member this.ParseBooleanWorks () =
        let falseTest = TwitterXMLParser.ParseBoolean(Data.booleanFalseXML, "enclosing")
        Assert.AreEqual(false, falseTest)
        let trueTest = TwitterXMLParser.ParseBoolean(Data.booleanTrueXML, "enclosing")
        Assert.AreEqual(true, trueTest)